import { createLogger } from 'redux-logger';
import { message } from 'antd';

import { RequestConfig } from 'umi';
import {getCookie} from '@/utils/cookie';

const baseURL = 'http://123.206.55.50:7002';  //后台设置了cors


export const request: RequestConfig = {
    timeout: 5000,
    errorConfig: {},
    middlewares: [],
    //请求拦截器  携带token headers  get params post data
    requestInterceptors: [ 
        (url, options) => {
            let token = getCookie('token') as string;
            return {
              url: `${baseURL+url}`,
              options: { ...options, interceptors: true,headers:{...options.headers,authorization:token} },
            };
        }
    ],
    //响应拦截器  集中处理错误信息
    responseInterceptors: [
        response => {
            const codeMaps:{[propName:number]:string} = {
                401: '鉴权失败',
                403: '请求携带信息有误',
                404: '资源不存在',
                500: '服务器有误',
                502: '网关错误。',
                503: '服务不可用，服务器暂时过载或维护。',
                504: '网关超时。',
            };
            if(response.status !== 200){
                message.error(codeMaps[response.status]);
            }
            return response;
        }
    ],
};

export const dva = {
  config: {
    onAction: createLogger(),
    onError(e: Error) {
      message.error(e.message, 3);
    },
  },
};